package com.demo.zyt.modules.app.config;

import java.io.File;
import java.security.GeneralSecurityException;
import java.util.Properties;

import javax.mail.internet.MimeMessage;

import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import com.sun.mail.util.MailSSLSocketFactory;

import lombok.extern.slf4j.Slf4j;

@Component
@Slf4j
public class ExecutorTask {
	
	
	
	private static String auth = "true";
	private static String host = "smtp.qq.com";
	private static String protocol = "smtp";
	private static int port = 465;
	private static String authName = "t-junwen@foxmail.com";
	private static String password = "fcfpssbrbiqhhchj";
	private static boolean isSSL = true;
	private static String charset = "UTF-8";
	private static String timeout = "5000";
	
//	@Autowired
//    private JavaMailSender mailSender;

	@Async("processExecutor")
	public void sendMail(String subject, String[] toUsers, String[] ccUsers, String content) {
		log.info("task start..");
		
		boolean flag = true;
		try {
			JavaMailSenderImpl javaMailSender = new JavaMailSenderImpl();
			javaMailSender.setHost(host);
			javaMailSender.setUsername(authName);
			javaMailSender.setPassword(password);
			javaMailSender.setDefaultEncoding(charset);
			javaMailSender.setProtocol(protocol);
			javaMailSender.setPort(port);

			Properties properties = new Properties();
			properties.setProperty("mail.smtp.auth", auth);
			properties.setProperty("mail.smtp.timeout", timeout);
			if (isSSL) {
				MailSSLSocketFactory sf = null;
				try {
					sf = new MailSSLSocketFactory();
					sf.setTrustAllHosts(true);
					properties.put("mail.smtp.ssl.enable", "true");
					properties.put("mail.smtp.ssl.socketFactory", sf);
				} catch (GeneralSecurityException e) {
					e.printStackTrace();
				}
			}
			javaMailSender.setJavaMailProperties(properties);

			MimeMessage mailMessage = javaMailSender.createMimeMessage();
			MimeMessageHelper messageHelper = new MimeMessageHelper(mailMessage, true);
			messageHelper.setTo(toUsers);
			if (ccUsers != null && ccUsers.length > 0) {
				messageHelper.setCc(ccUsers);
			}
			messageHelper.setFrom(authName);
			messageHelper.setSubject(subject);
			
			messageHelper.setText(content, true);
			log.warn("邮件内容:{}",content);
			javaMailSender.send(mailMessage);

		} catch (Exception e) {
			log.error("发送邮件失败!", e);
			flag = false;
		}
		
		log.info("task end...");
	}
	
	@Async("processExecutor")
	public void sendAttachmentsMail(String to, String subject, String content, String filePath) {
        // MimeMessage message=mailSender.createMimeMessage();
		
		
        
        JavaMailSenderImpl javaMailSender = new JavaMailSenderImpl();
		javaMailSender.setHost(host);
		javaMailSender.setUsername(authName);
		javaMailSender.setPassword(password);
		javaMailSender.setDefaultEncoding(charset);
		javaMailSender.setProtocol(protocol);
		javaMailSender.setPort(port);

		Properties properties = new Properties();
		properties.setProperty("mail.smtp.auth", auth);
		properties.setProperty("mail.smtp.timeout", timeout);
		if (isSSL) {
			MailSSLSocketFactory sf = null;
			try {
				sf = new MailSSLSocketFactory();
				sf.setTrustAllHosts(true);
				properties.put("mail.smtp.ssl.enable", "true");
				properties.put("mail.smtp.ssl.socketFactory", sf);
			} catch (GeneralSecurityException e) {
				e.printStackTrace();
			}
		}
		javaMailSender.setJavaMailProperties(properties);
		
		MimeMessage mailMessage = javaMailSender.createMimeMessage();
		// MimeMessageHelper messageHelper = new MimeMessageHelper(mailMessage, true);
		
        try {
            MimeMessageHelper helper=new MimeMessageHelper(mailMessage,true);
            helper.setFrom(authName);
            helper.setTo(to);
            helper.setSubject(subject);
            helper.setText(content);
            FileSystemResource file=new FileSystemResource(new File(filePath));
            String fileName = "粉丝列表.xls";
            helper.addAttachment(fileName,file);
            javaMailSender.send(mailMessage);
            System.out.println("带附件的邮件发送成功");
            
            File dfile = new File(filePath);
            boolean delFlag = dfile.delete();
            System.out.println("临时文件删除标记: " + delFlag);
        }catch (Exception e){
            e.printStackTrace();
            System.out.println("发送带附件的邮件失败");
        }
    }
	
}
